﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web;
namespace ArticleWeb.Common
{
    public class HttpWebResponseUtility
    {
        private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

        public static string CreateGetHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, string ip, CookieCollection cookies = null)
        {
            Encoding uTF = Encoding.UTF8;
            bool flag = string.IsNullOrEmpty(url);
            if (flag)
            {
                throw new ArgumentNullException("url");
            }
            StringBuilder stringBuilder = new StringBuilder();
            bool flag2 = parameters != null && parameters.Count != 0;
            if (flag2)
            {
                int num = 0;
                foreach (string current in parameters.Keys)
                {
                    bool flag3 = num > 0;
                    if (flag3)
                    {
                        stringBuilder.AppendFormat("&{0}={1}", current, parameters[current]);
                    }
                    else
                    {
                        stringBuilder.AppendFormat("{0}={1}", current, parameters[current]);
                    }
                    num++;
                }
            }
            bool flag4 = stringBuilder.Length > 1;
            if (flag4)
            {
                url = url + "?" + stringBuilder.ToString();
            }
            url = HttpUtility.UrlDecode(url);
            HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.Method = "GET";
            httpWebRequest.UserAgent = ((!string.IsNullOrEmpty(userAgent)) ? userAgent : HttpWebResponseUtility.DefaultUserAgent);
            httpWebRequest.Headers.Add("sign", "SDFGHJKL23456789#POIUYHCFTDFGHJKEHL");
            httpWebRequest.Headers.Add("ip", ip);
            httpWebRequest.Headers.Add("MebVersion", "1");
            httpWebRequest.Headers.Add("DevicePlatform", "info");
            bool flag5 = !string.IsNullOrEmpty(userAgent);
            if (flag5)
            {
                httpWebRequest.UserAgent = userAgent;
            }
            bool hasValue = timeout.HasValue;
            if (hasValue)
            {
                httpWebRequest.Timeout = timeout.Value;
            }
            bool flag6 = cookies != null;
            if (flag6)
            {
                httpWebRequest.CookieContainer = new CookieContainer();
                httpWebRequest.CookieContainer.Add(cookies);
            }
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), uTF);
            return streamReader.ReadToEnd();
        }

        public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, string ip, CookieCollection cookies)
        {
            bool flag = string.IsNullOrEmpty(url);
            if (flag)
            {
                throw new ArgumentNullException("url");
            }
            bool flag2 = requestEncoding == null;
            if (flag2)
            {
                throw new ArgumentNullException("requestEncoding");
            }
            HttpWebRequest httpWebRequest = null;
            bool flag3 = url.StartsWith("https", StringComparison.OrdinalIgnoreCase);
            if (flag3)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(HttpWebResponseUtility.CheckValidationResult);
                httpWebRequest = (WebRequest.Create(url) as HttpWebRequest);
                httpWebRequest.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                httpWebRequest = (WebRequest.Create(url) as HttpWebRequest);
            }
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.UserAgent = ((!string.IsNullOrEmpty(userAgent)) ? userAgent : HttpWebResponseUtility.DefaultUserAgent);
            httpWebRequest.Headers.Add("sign", "SDFGHJKL23456789#POIUYHCFTDFGHJKEHL");
            httpWebRequest.Headers.Add("DevicePlatform", "zs");
            bool flag4 = !string.IsNullOrEmpty(userAgent);
            if (flag4)
            {
                httpWebRequest.UserAgent = userAgent;
            }
            else
            {
                httpWebRequest.UserAgent = HttpWebResponseUtility.DefaultUserAgent;
            }
            bool hasValue = timeout.HasValue;
            if (hasValue)
            {
                httpWebRequest.Timeout = timeout.Value;
            }
            bool flag5 = cookies != null;
            if (flag5)
            {
                httpWebRequest.CookieContainer = new CookieContainer();
                httpWebRequest.CookieContainer.Add(cookies);
            }
            bool flag6 = parameters != null && parameters.Count != 0;
            if (flag6)
            {
                StringBuilder stringBuilder = new StringBuilder();
                int num = 0;
                foreach (string current in parameters.Keys)
                {
                    bool flag7 = num > 0;
                    if (flag7)
                    {
                        stringBuilder.AppendFormat("&{0}={1}", current, parameters[current]);
                    }
                    else
                    {
                        stringBuilder.AppendFormat("{0}={1}", current, parameters[current]);
                    }
                    num++;
                }
                byte[] bytes = requestEncoding.GetBytes(stringBuilder.ToString());
                using (Stream requestStream = httpWebRequest.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                }
            }
            return httpWebRequest.GetResponse() as HttpWebResponse;
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }
}